<?php

	class Category extends CI_Model
	{
		function __construct()
		{
			parent::__construct();
		}
		
		function getCategory($id = false)
		{			
			$query = $this->db->get_where('categories',array('id'=>$id));
			return $query->row_array();
		}

		function getAllCategories($id = false)
		{
			$query = $this->db->get('categories');
			//fix quick patch for category `sua chua`
			if($id == 34)
			{
				$query = $this->db->get_where('categories',array('id'=>34));
			}
			return $query->result_array();
		}

		function addCategory($data)
		{			
			$this->db->insert('categories',$data);
		}

		function updateCategory($data = false,$id = false)
		{
			$this->db->where('id',$id);
			return $this->db->update('categories',$data);			
		}

		function showProductsOfCategory($id,$num_of_products = false,$homedisplay = false)
		{
			$this->db->select('*');
			$this->db->from('products');			
			$this->db->where('category_id',$id);			

			if($num_of_products)
			{
				$this->db->limit(10,$num_of_products);
			}
			else if($homedisplay)
			{
				$this->db->limit(9,0);	
			}
			else
			{
				$this->db->limit(10,0);
			}

			$query = $this->db->get();
			return $query->result_array();
		}

		function getHomeCategory()
		{
			$query = $this->db->get_where('categories',array('homepage'=>'1'));
			return $query->result_array();
		}

		function getProductsOfHomeCategory()
		{						
			$categories = $this->getHomeCategory();
			$id = 0;
			foreach ($categories as $category) {								
				$data['category'][$id] = $category;
				$data['category'][$id]['products'] = $this->showProductsOfCategory($category['id'],0,1);
				$id++;
			}					

			return $data;
		}
				
	}